#include <stdio.h>
#include "ohos_init.h"
#include "los_event.h"
#include "los_task.h"
#include "securec.h"
#include "cmsis_os2.h"

osThreadId_t g_testTaskHandle;

osEventFlagsId_t g_exampleEvent;

#define EVENT_WAIT 0x00000001

void Example_Event(void *argument)
{
    uint32_t event;
    
    printf("Example_Event wait event 0x%x \n", EVENT_WAIT);
    
    event = osEventFlagsWait(g_exampleEvent, EVENT_WAIT, osFlagsWaitAll, 100);
    if (event & EVENT_WAIT) {
        printf("Example_Event,read event :0x%x\n", event);
    } else {
        printf("Example_Event,read event timeout\n");
    }
}

void Example_TaskEntry(void)
{
    
    g_exampleEvent = osEventFlagsNew(NULL);
    if (g_exampleEvent == NULL) {
        printf("init event failed.\n");
        return;
    }

    
    const osThreadAttr_t task_attr = {
        .name = "EventTsk1",
        .stack_size = 0x1000,
        .priority = osPriorityNormal  
    };

    
    g_testTaskHandle = osThreadNew(Example_Event, NULL, &task_attr);
    if (g_testTaskHandle == NULL) {
        printf("task create failed.\n");
        return;
    }

    
    printf("Example_TaskEntry write event.\n");
    if (osEventFlagsSet(g_exampleEvent, EVENT_WAIT) & EVENT_WAIT) {
        printf("Event set success\n");
    } else {
        printf("event write failed.\n");
    }

    
    printf("EventMask:%lu\n", osEventFlagsGet(g_exampleEvent));
    osEventFlagsClear(g_exampleEvent, osEventFlagsGet(g_exampleEvent));
    printf("EventMask:%lu\n", osEventFlagsGet(g_exampleEvent));

   
    osStatus_t status = osThreadTerminate(g_testTaskHandle);
    if (status != osOK) {
        printf("task delete failed.\n");
    }

    
    osEventFlagsDelete(g_exampleEvent);
}

APP_FEATURE_INIT(Example_TaskEntry);